<?php

namespace catchAdmin\stock\model;
use catchAdmin\stock\model\search\MoveMainSearch;
use catcher\base\CatchModel;
use think\facade\Db;

/**
 *
 * @property int $id
 * @property string $move_code
 * @property int $number
 * @property int $move_status
 * @property int $tenant_id
 * @property int $creator_id
 * @property int $deleted_at
 * @property int $created_at
 * @property int $updated_at
 * @property int $move_number
 */
class MoveMain extends CatchModel
{
    use MoveMainSearch;
    
    public $field = [
        //
        'id',
        // 移库主单编号
        'move_code',
        // 移库总数量
        'number',
        // 0未移库1部分移库2全部移库
        'move_status',
        //
        'tenant_id',
        //
        'creator_id',
        //
        'deleted_at',
        //
        'created_at',
        //
        'updated_at',
        // 移动数量
        'move_number',
        'edi_json',
        'edi_return',
        'customer_business_number'//业务单号
    ];
    
    public $name = 'db_move_main';
    /**
     * 列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getList($tenant_id=0)
    {
        $where = [];
        $where[] = ['deleted_at','=',0];
        if($tenant_id){
            $where[] = ['tenant_id','=',$tenant_id];
        }
        return $this->withoutField(['updated_at'], true)
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item){
                $item['username'] = Db::table('users')
                    ->where(['id' => $item['creator_id']])
                    ->value('username');
            });
    }

    public static function onAfterInsert($model){
//        //查询当月移库数量
//        $this_month_count =  Db::table('db_move_main')->whereMonth('created_at')->count();
//
//        //移库单证
//        $model->move_code = "CIWHMV"  . date("ym") . str_pad($this_month_count,5,'0',STR_PAD_LEFT);
//        $model->save();
    }
}